﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;

namespace HCFramework.Product.Confirmation.Control
{
    public partial class Order_ProductsInfo : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadGridMain.MasterTableView.NoMasterRecordsText = M.Common.GetLang(RadGridMain.MasterTableView.NoMasterRecordsText);
                foreach (GridColumn col in RadGridMain.Columns)
                {
                    col.HeaderText = M.Common.GetLang(col.HeaderText);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            //LoadGridData();
            
        }
        public void BindData(Order order)
        {
            litOrderId.Text = order.OrderID + "";
            HCDataContext db = new HCDataContext();
            var result = from a in db.Order_Products
                         where a.OrderID == order.OrderID
                         select new
                         {
                             a.CurrencyCode,
                             a.OrderID,
                             a.ProductID,
                             a.Price,
                             a.Quantity,
                             a.Product.ProductCode,
                             a.Product.ProductName,
                         };
            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //if (e.CommandName == "Xoa")
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    RoleController.Delete(Guid.Parse(item["RoleID"].Text));
            //}
        }

        protected void RadGridMain_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int productId = int.Parse((e.Item as GridDataItem)["ProductId"].Text);
                Literal litCount = e.Item.FindControl("litCount") as Literal;
                Literal litPrice = e.Item.FindControl("litPrice") as Literal;
                Literal litTotalPrice = e.Item.FindControl("litTotalPrice") as Literal;
                litTotalPrice.Text = (int.Parse(litCount.Text) * decimal.Parse(litPrice.Text)).ToString("N0");
                litPrice.Text = decimal.Parse(litPrice.Text).ToString("N0");

                HyperLink linkProductDetail = e.Item.FindControl("linkProductDetail") as HyperLink;
                linkProductDetail.NavigateUrl = "/Product/Detail/Default.aspx?productID=" + productId;
            }
            if (e.Item.ItemType == GridItemType.Footer)
            {
                int orderId = int.Parse(litOrderId.Text);
                Literal litTotalPrice = e.Item.FindControl("litTotalPrice") as Literal;
                litTotalPrice.Text = C.OrderController.GetTotalPrice(orderId).ToString("N0");
            }
        }
    }
}